package com.kakaxi.mybilibili.module.home.category.presenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kakaxi.mybilibili.adapter.entity.CategoryAreaFirstMultiItem;
import com.kakaxi.mybilibili.bean.BannerItemBean;
import com.kakaxi.mybilibili.bean.CategoryAreaVideoItemBean;
import com.kakaxi.mybilibili.common.WebParamConst;
import com.kakaxi.mybilibili.module.home.category.model.CategoryAreaFirstModel;
import com.kakaxi.mybilibili.module.home.category.model.ICategoryAreaFirstModel;
import com.kakaxi.mybilibili.module.home.category.view.ICategoryAreaFirstView;
import com.vincestyling.netroid.Listener;
import com.vincestyling.netroid.NetroidError;

import java.util.ArrayList;
import java.util.List;

/**
 * 分类分区首个碎片控制层实现类
 * Created by kakaxi on 16/11/29.
 */

public class CategoryAreaFirstPresenter implements ICategoryAreaFirstPresenter {

    private ICategoryAreaFirstModel mModel;

    private ICategoryAreaFirstView mView;

    public CategoryAreaFirstPresenter(ICategoryAreaFirstView categoryAreaFirstView) {
        mView = categoryAreaFirstView;
        mModel = new CategoryAreaFirstModel();
    }

    @Override
    public void initCategoryAreaFirstData(int tid) {
        mView.showLoading();
        getCategoryAreaFirstData(tid);
    }

    @Override
    public void refreshCategoryAreaFirstData(int tid) {
        getCategoryAreaFirstData(tid);
    }

    /**
     * 获取数据
     *
     * @param tid id
     */
    private void getCategoryAreaFirstData(final int tid) {
        mModel.getCategoryAreaFirstData(String.valueOf(tid), new Listener<JSONObject>() {
            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
                if (response.getString(WebParamConst.RESPONSE_CODE).equals(WebParamConst
                        .RESULT_CODE_OK)) {
                    // 展示
                    mView.showCategoryAreaFirstData(parse(response));
                    // 调用获取下面的所有动态视频数据
                    getCategoryAreaFirstAllData(tid);
                } else {
                    mView.hideLoading();
                    mView.showEmptyView();
                }
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
                mView.hideLoading();
            }
        });
    }

    /**
     * 转化为适配器需要的列表
     *
     * @param response 响应
     * @return 列表数组
     */
    private List<CategoryAreaFirstMultiItem> parse(JSONObject response) {
        JSONObject result = response.getJSONObject(WebParamConst.RESPONSE_RESULT);
        // 最新视频
        List<CategoryAreaVideoItemBean> newsList = JSON.parseArray(result.getJSONArray
                (WebParamConst.RESPONSE_DATA_CATEGORY_NEWS).toString(), CategoryAreaVideoItemBean
                .class);
        // 热门推荐
        List<CategoryAreaVideoItemBean> recommendsList = JSON.parseArray(result.getJSONArray
                        (WebParamConst.RESPONSE_DATA_CATEGORY_RECOMMENDS).toString(),
                CategoryAreaVideoItemBean.class);
        // 轮播
        List<BannerItemBean> bannersList = JSON.parseArray(result.getJSONArray(WebParamConst
                .RESPONSE_DATA_CATEGORY_BANNERS).toString(), BannerItemBean.class);

        // 把数组转化成复杂列表项
        List<CategoryAreaFirstMultiItem> multiItemList = new ArrayList<>();
        multiItemList.add(new CategoryAreaFirstMultiItem(CategoryAreaFirstMultiItem
                .TYPE_HEADER_BANNER, bannersList));
        multiItemList.add(new CategoryAreaFirstMultiItem(CategoryAreaFirstMultiItem
                .TYPE_HEADER_BUTTON_BAR, mView.getButtonBarList()));
        multiItemList.add(new CategoryAreaFirstMultiItem(CategoryAreaFirstMultiItem
                .TYPE_HEADER_RECOMMEND, "热门推荐"));
        for (CategoryAreaVideoItemBean itemBean : recommendsList) {
            multiItemList.add(new CategoryAreaFirstMultiItem(CategoryAreaFirstMultiItem
                    .TYPE_CONTENT, itemBean));
        }
        multiItemList.add(new CategoryAreaFirstMultiItem(CategoryAreaFirstMultiItem
                .TYPE_HEADER_NEW, "最新视频"));
        for (CategoryAreaVideoItemBean itemBean : newsList) {
            multiItemList.add(new CategoryAreaFirstMultiItem(CategoryAreaFirstMultiItem
                    .TYPE_CONTENT, itemBean));
        }

        return multiItemList;
    }

    /**
     * 获取所有动态视频数据
     *
     * @param tid id
     */
    private void getCategoryAreaFirstAllData(int tid) {
        mModel.getCategoryAreaFirstAllData(String.valueOf(tid), new Listener<JSONObject>() {
            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
                mView.hideLoading();
                if (response.getString(WebParamConst.RESPONSE_CODE).equals(WebParamConst
                        .RESULT_CODE_OK)) {
                    mView.showCategoryAreaFirstAllData(parseAll(response));
                }
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
                mView.hideLoading();
            }
        });
    }

    /**
     * 转换所有动态视频
     *
     * @param response 响应
     * @return 数组
     */
    private List<CategoryAreaFirstMultiItem> parseAll(JSONObject response) {
        JSONArray listJsonArr = response.getJSONArray(WebParamConst.RESPONSE_LIST);
        List<CategoryAreaVideoItemBean> list = JSON.parseArray(listJsonArr.toString(),
                CategoryAreaVideoItemBean.class);

        // 把数组转化成复杂列表项
        List<CategoryAreaFirstMultiItem> multiItemList = new ArrayList<>();
        multiItemList.add(new CategoryAreaFirstMultiItem(CategoryAreaFirstMultiItem
                .TYPE_HEADER_ALL, "全区动态"));
        for (CategoryAreaVideoItemBean itemBean : list) {
            multiItemList.add(new CategoryAreaFirstMultiItem(CategoryAreaFirstMultiItem
                    .TYPE_CONTENT, itemBean));
        }
        return multiItemList;
    }


}
